(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

type +'a io = {
  (* general *)
  io_free : unit -> unit;

  (* reading *)
  io_read_char : unit -> char;
  io_read_string : int -> string;
  io_eof : unit -> bool;

  (* writing *)
  io_write_char : char -> unit;
  io_write_string : string -> unit;
  io_bytes_written : unit -> int;
  
  (* random access *)
  io_size : unit -> int;
  io_pos : unit -> int;
  io_seek : int -> unit
}

type io_r = [ | `IO_Read ]
type io_w = [ | `IO_Write ]
type io_s = [ | `IO_Seek ]
type io_rs = [ | io_r | io_s ]
type io_ws = [ | io_w | io_s ]
type io_rw = [ | io_r | io_w ]
type io_rws = [ | io_r | io_w | io_s ]

type istream = io_r io
type irstream = io_rs io
type ostream = io_w io
type orstream = io_ws io
type iostream = io_rw io
type iorstream = io_rws io

let not_implemented _ = assert false
  
let io_make fr rc rs eof wc ws bw sz pos sk =
  {
    io_free = fr;
    io_read_char = rc;
    io_read_string = rs;
    io_eof = eof;
    io_write_char = wc;
    io_write_string = ws;
    io_bytes_written = bw;
    io_size = sz;
    io_pos = pos;
    io_seek = sk;
  }
  
let io_make_read fr rc rs eof =
  {
    io_free = fr;
    io_read_char = rc;
    io_read_string = rs;
    io_eof = eof;
    io_write_char = not_implemented;
    io_write_string = not_implemented;
    io_bytes_written = not_implemented;
    io_size = not_implemented;
    io_pos = not_implemented;
    io_seek = not_implemented;
  }
  
let io_make_write fr wc ws bw =
  {
    io_free = fr;
    io_read_char = not_implemented;
    io_read_string = not_implemented;
    io_eof = not_implemented;
    io_write_char = wc;
    io_write_string = ws;
    io_bytes_written = bw;
    io_size = not_implemented;
    io_pos = not_implemented;
    io_seek = not_implemented;
  }
  
let io_make_read_seek fr rc rs eof sz pos sk =
  {
    io_free = fr;
    io_read_char = rc;
    io_read_string = rs;
    io_eof = eof;
    io_write_char = not_implemented;
    io_write_string = not_implemented;
    io_bytes_written = not_implemented;
    io_size = sz;
    io_pos = pos;
    io_seek = sk;
  }
  
let io_make_write_seek fr wc ws bw sz pos sk =
  {
    io_free = fr;
    io_read_char = not_implemented;
    io_read_string = not_implemented;
    io_eof = not_implemented;
    io_write_char = wc;
    io_write_string = ws;
    io_bytes_written = bw;
    io_size = sz;
    io_pos = pos;
    io_seek = sk;
  }
  
let io_make_read_write fr rc rs eof wc ws bw =
  {
    io_free = fr;
    io_read_char = rc;
    io_read_string = rs;
    io_eof = eof;
    io_write_char = wc;
    io_write_string = ws;
    io_bytes_written = bw;
    io_size = not_implemented;
    io_pos = not_implemented;
    io_seek = not_implemented;
  }
  
let io_free cs = cs.io_free ()
let io_read_char cs = cs.io_read_char ()
let io_read_string cs len = cs.io_read_string len
let io_eof cs = cs.io_eof ()
let io_write_char cs chr = cs.io_write_char chr
let io_write_string cs str = cs.io_write_string str
let io_bytes_written cs = cs.io_bytes_written ()
let io_size cs = cs.io_size ()
let io_pos cs = cs.io_pos ()
let io_seek cs off = cs.io_seek off
let io_coerce_i cs = (cs :> istream)
let io_coerce_o cs = (cs :> ostream)
let io_coerce_ir cs = (cs :> irstream)
let io_coerce_or cs = (cs :> orstream)
let io_coerce_io cs = (cs :> iostream)
let io_coerce_ior cs = (cs :> iorstream)
